<template>
  <header>
    <div>
      <span>TODOLIST</span>
      <input
        type="text"
        placeholder="添加toDo"
        v-model="inputval"
        @keydown.enter="add"
      />
    </div>
  </header>
  <div class="container">
    <div class="tasks">
      <div class="title-box">
        <h3>正在进行</h3>
        <span class="run-count">{{runLength}}</span>
      </div>
      <ul class="running-tasks-box">
        <!-- template 不渲染成标签 -->
        <template v-for="(item, index) in list">
          <li class="list-item" :key="index" v-if="item.flag == false">
            <div>
              <input type="checkbox" v-model="item.flag" />
              <span v-if="item.showSpan" @dblclick="item.showSpan = false">{{ item.msg }}</span>
              <input type="text" class="count_input" v-else v-model="item.msg" @blur="item.showSpan = true">
            </div>
            <div class="del" @click="del(index)">-</div>
          </li>
        </template>
      </ul>
    </div>
    <div class="tasks">
      <div class="title-box">
        <h3>已经完成</h3>
        <span class="finish-count">{{finishLength}}</span>
      </div>
      <ul class="finish-task-box">
        <template v-for="(item, index) in list">
          <li class="list-item" :key="index" v-if="item.flag">
            <div>
              <input type="checkbox" v-model="item.flag" />
              <span v-if="item.showSpan" @dblclick="item.showSpan = false">{{ item.msg }}</span>
              <input type="text" class="count_input" v-else v-model="item.msg" @blur="item.showSpan = true">
            </div>
            <div class="del" @click="del(index)">-</div>
          </li>
        </template>
      </ul>
    </div>
  </div>
</template>

<script>
// @ is an alias to /src

export default {
  name: "Home",
  components: {},
  data() {
    return {
      inputval: "",
      list: [], // 存储添加的数据
    };
  },
  computed:{
    runLength(){
      return this.list.filter(item => item.flag == false).length
    },
     finishLength(){
      return this.list.filter(item => item.flag).length
    }
  },
  methods: {
    add() {
      if (this.inputval) {
        this.list.push({
          msg: this.inputval,
          flag: false, // false 正在进行 true 已完成
          showSpan: true, // 是否显示 span标签
        });
        this.inputval = '';
      }
    },
    del(i){
      this.list.splice(i,1);
    }
  },
};
</script>
<style lang="scss" scoped>
header {
  width: 100%;
  height: 50px;
  background-color: #444;
}

header div {
  width: 600px;
  margin: 0 auto;
  height: 100%;
  /* 将元素盒转化为弹性盒子 */
  display: flex;
  /* 
            justify-content 主轴方向上的对齐方式
            space-between 两边对齐 中间留白
             */
  justify-content: space-between;
  /* 侧轴的对齐方式 */
  align-items: center;
  color: #fff;
}

header div span {
  font-size: 30px;
  font-weight: bold;
}

header div input {
  outline: none;
  border: none;
  width: 300px;
  height: 30px;
  padding-left: 15px;
  border-radius: 6px;
}

.container {
  width: 100%;
}

.tasks {
  width: 600px;
  margin: 0 auto;
  margin-top: 50px;
}

ul {
  list-style: none;
  padding: 0;
  margin: 0;
}

.title-box {
  width: 100%;
  display: flex;
  justify-content: space-between;
  align-items: center;
}

h3 {
  font-size: 26px;
  font-weight: bold;
}

.title-box span {
  width: 20px;
  height: 20px;
  line-height: 20px;
  text-align: center;
  background-color: #fff;
  border-radius: 20px;
}

.list-item {
  width: 100%;
  height: 40px;
  display: flex;
  justify-content: space-between;
  align-items: center;
  background-color: #fff;
  border-left: 4px solid #629a9c;
  border-radius: 6px;
  padding-left: 10px;
  padding-right: 15px;
  margin-bottom: 20px;
}

.list-item input {
  height: 30px;
  width: 30px;
  vertical-align: middle;
}
.list-item .count_input{
  width: 200px;
}
.list-item span {
  display: inline-block;
  vertical-align: middle;
}

.del {
  box-sizing: content-box;
  display: inline-block;
  width: 14px;
  height: 12px;
  border-radius: 14px;
  border: 6px double #fff;
  background: #ccc;
  line-height: 14px;
  text-align: center;
  color: #fff;
  font-weight: bold;
  font-size: 14px;
  cursor: pointer;
}

.finish-task-box {
  opacity: 0.5;
}
</style>
